import json
from jsonpath import jsonpath
from common.ds_create_headers import CreateHeaders
import requests


class ProcessJson:

    def __init__(self):
        self.create_headers = CreateHeaders()

    # 读取测试用例的json 文件
    def read_json(self):
        with open("../testcase/testcase.json", encoding="utf-8") as file:
            file_json = json.loads(file.read())
            return file_json

    # 获取测试用例数量
    def get_case_count(self):
        case_count = len(jsonpath(self.read_json(), "$.json_data[*].api.data"))
        return case_count

    # 获取API Address
    def get_case_url(self):
        case_urls = jsonpath(self.read_json(), "$.json_data[*].api.url")
        send_urls = []
        for case_url in case_urls:
            host = self.create_headers.get_host()
            if case_url[0:21] != host and 'dsers.com' not in case_url:
                send_url = host + case_url
                send_urls.append(send_url)
            else:
                send_urls = case_urls
        return send_urls

    # 获取所有测试用例请求方式
    def get_case_method(self):
        case_method = jsonpath(self.read_json(), "$.json_data[*].api.method")
        return case_method

    # 获取所有测试用例请求数据
    def get_case_data(self):
        data = jsonpath(self.read_json(), "$..json_data[*].api.data")
        return data

    # 获取接口预期结果（UI的测试预期不要写）
    def get_expected(self):
        expected = jsonpath(self.read_json(), "$..json_data[*].expected")
        return expected

    # 获取需要缓存的字段
    def get_cache_data(self):
        cache = jsonpath(self.read_json(), "$..json_data[*].cache")
        return cache

    # 缓存字段之后，把对应的数据写入文件
    def write_cache_data(self, response, num):
        response_json = response.json()
        cache_data_list = self.get_cache_data()[num]
        for cache_datas in cache_data_list:
            cache_data = jsonpath(response_json, '$..{}'.format(cache_datas))
            config_cache_data = jsonpath(self.create_headers.get_cache_data(), "$..cache_data")
            print(config_cache_data)
            with open("../config/data.json", "w", encoding='utf8') as file:
                config_json = json.load(file)
                json.dump(config_json, )
            # print(test)


    def testreponse(self):
        url = "https://atc.dsers.com/uaa/api/1.0/login"

        payload = "{\n    \"cap\": \"no value\",\n    \"cap_key\": \"caemse8oatig01kridig\",\n    \"email\": \"testnew2@dsers.com\",\n    \"password\": \"123456\",\n    \"google_response\": \"\"\n}"
        headers = {
            'accept': "application/json",
            'Content-Type': "application/json",
            'User-Agent': "PostmanRuntime/7.19.0",
            'Cache-Control': "no-cache",
            'Postman-Token': "6acbc487-c56d-48c4-a8d8-f8e819c6e411,01fb2fe8-e386-4e7e-a371-718456131f3b",
            'Host': "atc.dsers.com",
            'Accept-Encoding': "gzip, deflate",
            'Content-Length': "152",
            'Connection': "keep-alive",
            'cache-control': "no-cache"
        }

        response = requests.request("POST", url, data=payload, headers=headers)
        return response
if __name__ == '__main__':
    process_json = ProcessJson()
    process_json.write_cache_data(process_json.testreponse(), num=0)
